<template>
  <div>
    <div class="divHead">
      <el-row :gutter="30">
        <el-col :span="6">
          <div class="divLable">仓库名称</div>
          <el-input v-model="getAreaPageData.warehouseName" placeholder="请输入"></el-input>
        </el-col>
        <el-col :span="6">
          <div class="divLable">库区名称</div>
          <el-input v-model="getAreaPageData.name" placeholder="请输入"></el-input>
        </el-col>
        <el-col :span="6">
          <div class="divLable">仓库状态</div>
          <el-select v-model="getAreaPageData.status" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <div class="divLable"></div>
          <div class="divButton">
            <el-button @click="getAreaPage()"> 搜索 </el-button>
            <el-button @click="reset"> 重置 </el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="divContent">
      <el-button class="btnAdd" @click="editor(null)">新增库区</el-button>
      <div class="textRight">
        <el-button class="unimportanceButton" @click="download" round>下载仓库模板</el-button>
        <el-button class="unimportanceButton" @click="addImport" round>导入库区信息</el-button>
      </div>
      <div>
        <el-table :data="tableData" style="width: 100%" :row-style="{ height: '44px' }" :cell-style="{ padding: '0px' }">
          <el-table-column type="index" label="序号" width="68"> </el-table-column>
          <el-table-column prop="warehouseName" label="所属仓库" width="150"> </el-table-column>
          <el-table-column prop="code" label="库区编号" width="150"> </el-table-column>
          <el-table-column prop="name" label="库区名称" width="150"> </el-table-column>
          <el-table-column prop="temperatureType" label="温度类型" width="200">
            <template slot-scope="scope">
              {{ scope.row.temperatureType === 'CW' ? '常温' : scope.row.temperatureType === 'LC' ? '冷藏' : '恒温' }}
            </template>
          </el-table-column>
          <el-table-column prop="bearingType" label="称重类型" width="150">
            <template slot-scope="scope">
              {{ scope.row.bearingType === 'BX' ? '中型' : scope.row.bearingType === 'ZX' ? '重型' : '轻型' }}
            </template>
          </el-table-column>
          <el-table-column prop="useType" label="用途属性" width="150">
            <template slot-scope="scope">
              {{ scope.row.useType === 'RKHCQ' ? '入库缓存区' : scope.row.useType === 'CKHCQ' ? '出库缓存区' : scope.row.useType === 'CCQ' ? '存储区' : scope.row.useType === 'FJQ' ? '分拣区' : '质检区' }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="库区状态" width="150">
            <template slot-scope="scope">
              {{ scope.row.status ? '启用' : '停用' }}
            </template>
          </el-table-column>
          <el-table-column prop="personName" label="负责人" width="150"> </el-table-column>
          <el-table-column prop="phone" label="联系电话" width="150"> </el-table-column>
          <el-table-column prop="includedNum" label="库位数量" width="150"> </el-table-column>
          <el-table-column prop="updateTime" label="更新时间" width="200"> </el-table-column>
          <el-table-column label="操作" fixed="right" width="180">
            <template slot-scope="scope">
              <div class="operate">
                <el-button type="text" @click="editor(scope.row.id)">编辑</el-button>
                <el-button type="text" @click="openDialog(scope.row.name, scope.row.status, scope.row.id)">{{ scope.row.status ? '停用' : '启用' }}</el-button>
                <el-button type="text" @click="openDialog(scope.row.name, 'del', scope.row.id)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination :page-sizes="[10, 20, 30, 40]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="+datas.total" :current-page="+datas.current" @size-change="handleSizeChange" @current-change="handleCurrentChange"> </el-pagination>
      </div>
    </div>
    <FnDialog ref="FnDialog" :dialog="dialog"></FnDialog>
    <AddImport ref="AddImport"></AddImport>
  </div>
</template>

<script>
import { getAreaPage, putArea, delArea, getAreaList } from '@/api/ManageBaseInfo'
import FnDialog from './components/FnDialog.vue'
import AddImport from './WareArea/AddImports.vue'
import exportExcelMapPath from '@/constent/exportExcelMapPath'
export default {
  components: {
    FnDialog,
    AddImport
  },
  data () {
    return {
      tableData: [],
      getAreaPageData: {
        id: '',
        name: '',
        warehouseName: '',
        status: '',
        current: 1,
        size: 10
      },
      datas: {},
      options: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 0,
          label: '停用'
        },
        {
          value: 1,
          label: '启用'
        }
      ],
      dialog: {
        title: '',
        text: ''
      },
      fnId: ''
    }
  },
  methods: {
    async getAreaPage () {
      if (this.getAreaPageData.status === '') {
        this.$delete(this.getAreaPageData, 'status')
      }
      const res = await getAreaPage(this.getAreaPageData)
      this.tableData = res.data.records
      this.datas = res.data
    },
    reset () {
      this.getAreaPageData = {
        id: '',
        name: '',
        warehouseName: '',
        current: 1,
        size: 10
      }
    },
    openDialog (name, fn, id) {
      this.dialog.title = fn === 'del' ? '确认删除' : fn ? '确认停用' : '确认启用'
      this.dialog.text = fn === 'del' ? `确认要删除：${name} 吗？` : fn ? `确认要停用：${name} 吗？` : `确认要启用：${name} 吗？`
      this.fnId = id
      this.$refs.FnDialog.dialogVisible = true
    },
    async del () {
      await delArea(this.fnId)
      this.getAreaPage()
      this.$refs.FnDialog.dialogVisible = false
    },
    async put (state) {
      await putArea({ id: this.fnId, status: state })
      this.getAreaPage()
      this.$refs.FnDialog.dialogVisible = false
    },
    editor (id) {
      this.$router.push({
        path: `WareArea/details/${id}`
      })
    },
    handleSizeChange (val) {
      this.getAreaPageData.size = val
      this.getAreaPage()
    },
    handleCurrentChange (val) {
      this.getAreaPageData.current = val
      this.getAreaPage()
    },
    // 下载表格
    async download () {
      const res = await getAreaList()
      const tHeader = Object.keys(exportExcelMapPath)
      const data = res.data.map((item) => {
        return tHeader.map((h) => {
          return item[exportExcelMapPath[h]]
        })
      })
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          header: tHeader, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx' // 非必填
        })
      })
    },
    addImport () {
      this.$refs.AddImport.dialogVisible = true
    }
  },
  created () {
    this.getAreaPage()
  }
}
</script>

<style lang="less" scoped>
/deep/ .divHead {
  box-sizing: border-box;
  height: 127px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
  padding: 30px;
  margin-bottom: 20px;
  .el-row {
    position: relative;
    box-sizing: border-box;
    .el-col {
      float: left;
      box-sizing: border-box;
      .divLable {
        margin-bottom: 10px;
        height: 18px;
        font-size: 12px;
        font-weight: 500;
        color: #887e7e;
      }
      input {
        height: 40px;
        line-height: 40px;
        border: 1px solid #f8f5f5;
        background: #f8f5f5;
        border-radius: 6px;
      }
      .el-select {
        width: 100%;
      }
      .divButton {
        float: right;
        button {
          border: none;
          border-radius: 20px;
          margin-right: 10px;
        }
        button:nth-child(1) {
          background: #ffb200;
          color: #332929;
        }
        button:nth-child(2) {
          background: #f8f5f5;
          color: #332929;
        }
      }
    }
  }
}
/deep/ .divContent {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
  .btnAdd {
    margin-left: 30px;
    margin-top: 20px;
    margin-bottom: 20px;
    background: #00be76;
    color: #fff;
    border-radius: 20px;
  }
  .textRight {
    float: right;
    margin-right: 30px;
    margin-top: 20px;
    .unimportanceButton {
      margin-left: 30px;
      background: #f8f5f5;
      color: #332929;
      border: none;
    }
    .unimportanceButton:hover {
      background-color: RGB(255, 178, 0);
    }
  }
  .el-table {
    .operate {
      button {
        color: #ffb200;
      }
    }
  }
  tr:nth-child(2n) {
    background-color: RGB(253, 252, 249);
  }
  th {
    background-color: RGB(249, 246, 238);
    text-align: center;
  }
  td {
    text-align: center;
  }
  .el-pagination {
    box-sizing: border-box;
    margin: 0 auto;
    text-align: center;
    padding-top: 18px;
    padding-bottom: 34px;
    input {
      background: #f8f5f5;
      border: 1px solid #f8f5f5;
    }
  }
}
</style>
